/**
 * 
 */
package com.zznode.client.wlanpolling.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lamerman.FileDialog;
import com.zznode.client.wlanpolling.R;
import com.zznode.client.wlanpolling.WLANPollingApplication;
import com.zznode.client.wlanpolling.WLanContext;
import com.zznode.client.wlanpolling.model.SystemSetting;
import com.zznode.client.wlanpolling.model.SystemSettingManager;

/**
 * 
 *
 */
public class SettingsActivity extends Activity {
	public static int REQUEST_SAVE = 1;
	public static int REQUEST_LOAD = 2;
	
	/**
	 * Ping参数设置
	 */
	private EditText etPingParam;
	
	private EditText etApPingParam;

	/**
	 * 宽带服务器地址
	 */
	private EditText etKuandaiServerAdress;

	/**
	 * ftp文件文件下载地址
	 */
	private EditText etUploadFileUrl;
	
	/**
	 * ftp默认测试次数
	 */
	private EditText etFTPTestCount;

	/**
	 * 关联次数
	 */
	private EditText etRelateTimes;
	private EditText etReassociateTimeout;
	
	/**
	 * 认证次数
	 */
	private EditText etWebAuthTimes;
	private EditText etThroughtoutputTestUrl;
	
	private Button btnSaveSetting;

	private WLANPollingApplication application;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		application = (WLANPollingApplication) getApplication();

		bindView();
		bindEvent();
	}

	private void bindView() {
		etPingParam = (EditText) findViewById(R.id.pingParam);
		etKuandaiServerAdress = (EditText) findViewById(R.id.kuandaiServerAdress);
		etUploadFileUrl = (EditText) findViewById(R.id.ftpFilePath);
		etFTPTestCount = (EditText) findViewById(R.id.ftpTestCount);
		etRelateTimes = (EditText) findViewById(R.id.relateTimes);
		etWebAuthTimes = (EditText) findViewById(R.id.webAuthTimes);
		etThroughtoutputTestUrl = (EditText)findViewById(R.id.throughtoutputTestUrl);
		etReassociateTimeout = (EditText)findViewById(R.id.reassociateTimeout);
		etApPingParam = (EditText)findViewById(R.id.apPingParam);
		
		WLanContext context = application.getContext();
		SystemSetting setting = context.getSystemSetting();
		if (setting != null) {
			etPingParam.setText(setting.getPingParams());
			etApPingParam.setText(setting.getApPingParams());
			etKuandaiServerAdress.setText(setting.getAdslServerAdress());
			etUploadFileUrl.setText(setting.getUploadFileUrl());
			etFTPTestCount.setText(setting.getFtpTestCount() + "");
			etRelateTimes.setText(setting.getRelateTestTimes() + "");
			etWebAuthTimes.setText(setting.getWebAuthTimes() + "");
			etReassociateTimeout.setText(setting.getReassociateTimeout() + "");
		}

		btnSaveSetting = (Button) findViewById(R.id.btnSaveSetting);
	}

	private void bindEvent() {
		btnSaveSetting.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				WLanContext context = application.getContext();
				SystemSetting setting = context.getSystemSetting();
				if (setting == null) {
					setting = new SystemSetting();
				}

				setting.setPingParams(etPingParam.getText().toString());
				setting.setApPingParams(etApPingParam.getText().toString());
				setting.setAdslServerAdress(etKuandaiServerAdress.getText()
						.toString());
				setting.setUploadFileUrl(etUploadFileUrl.getText().toString());
				String count = etFTPTestCount.getText().toString();
				if(count != null && count.length() > 0){
					setting.setFtpTestCount(Integer.parseInt(count));
				}
				if (etRelateTimes.getText() != null
						&& etRelateTimes.getText().length() > 0) {
					setting.setRelateTestTimes(Integer.parseInt(etRelateTimes
							.getText().toString()));
				} else {
					setting.setRelateTestTimes(0);
				}

				if (etWebAuthTimes.getText() != null
						&& etWebAuthTimes.getText().length() > 0) {
					setting.setWebAuthTimes((Integer.parseInt(etWebAuthTimes
							.getText().toString())));
				} else {
					setting.setRelateTestTimes(0);
				}
				
				if(etReassociateTimeout.getText() != null &&
						etReassociateTimeout.getText().length() > 0) {
					setting.setReassociateTimeout(Integer.parseInt(etReassociateTimeout.getText().toString()));
				} else {
					setting.setReassociateTimeout(90);
				}
				
				if(etThroughtoutputTestUrl.getText() != null &&
						etThroughtoutputTestUrl.getText().length() > 0) {
					setting.setThroughtoutputTestUrl(etThroughtoutputTestUrl.getText().toString());
				}

				SystemSettingManager systemSettingManager = new SystemSettingManager(
						getApplicationContext());
				systemSettingManager.saveSystemSetting(setting);

				Toast.makeText(SettingsActivity.this, "系统设置保存成功!",
						Toast.LENGTH_LONG).show();
			}
		});

		Button btnBrowserFile = (Button) findViewById(R.id.btnBrowserFile);
		btnBrowserFile.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//
				Intent intent = new Intent(SettingsActivity.this, FileDialog.class);
				intent.putExtra(FileDialog.START_PATH, "/sdcard");
				intent.putExtra(FileDialog.CAN_SELECT_DIR, false);
				// intent.putExtra(FileDialog.FORMAT_FILTER, new String[] {"png" });
				SettingsActivity.this.startActivityForResult(intent, REQUEST_LOAD);
				//((TabActivity)getParent()).start
				//startActivityForResult(intent, REQUEST_LOAD);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode,
			int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {

			if (requestCode == REQUEST_SAVE) {
				System.out.println("Saving...");
			} else if (requestCode == REQUEST_LOAD) {
				System.out.println("Loading...");
			}

			String filePath = data.getStringExtra(FileDialog.RESULT_PATH);
			etUploadFileUrl.setText(filePath);
		} else  {
			//(resultCode == Activity.RESULT_CANCELED)
			Log.d(SettingsActivity.class.getName(), "file not selected, requestCode is : " + resultCode);
		}
	}
	
	@Override
	public void onBackPressed() {
		if(getParent() instanceof MainTabActivity) {
			((MainTabActivity)getParent()).changeTabByTabIndex(0);
		} else {
			super.onBackPressed();
		}
	}
}
